staticpage.admin = liwe.module ( 'staticpage.admin', null, 'Pagine Statiche' ); 

staticpage.admin._dest = '';
staticpage.admin.admin_man = null;

staticpage.admin.init = function ( dest_div )
{
	if ( ! staticpage.admin.templates )
	{
		liwe.AJAX.easy ( { action: "staticpage.ajax.get_templates" }, function ( v ) {
			staticpage.admin.templates = v [ 'templates' ];
			staticpage.admin._admin_manager_init ( dest_div );
		} );
	}

	var _acts = [
			{ func: function () { staticpage.admin.admin_man.set_dest_div ( 'system-content'); staticpage.admin.set ( 'system-content' ); } , img: '', label: 'Nuova pagina', "class" : '' },
			{ func: function () { staticpage.admin.admin_man.set_dest_div ( 'system-content'); staticpage.admin.list (); } , img: '', label: 'Lista Pagine', "class" : '' }
			]; 

        var _plugin = [
                {
                        label:   "Inserisci Pagina Statica",
                        handler: staticpage.admin._jconed_handler,
                        context: staticpage.admin._jconed_context
                }
        ];
	staticpage.admin.register ( _acts, _plugin );
};

staticpage.admin._admin_manager_init = function ( dest_div )
{
	if ( ! staticpage.admin.admin_man )
	{
		staticpage.admin.admin_man = new AdminManager ( { 
			'dest_res': dest_div,
			'list_action': "staticpage.admin.list",
			'del_action': 'staticpage.admin.del' ,
			'mod_menu_actions': [
			]
		} );

		staticpage.admin.admin_man.set_row_fields ( 
					[
						[ "ID", "id" ],
						[ "Nome", "name" ],
						[ "Titolo", "title" ],
						[ "Visibile", "_visible" ],
						[ "Data creazione", "_created" ]
					]
					);


		staticpage.admin.admin_man.cbacks [ 'row_manip' ] = staticpage.admin.list_row_manip;
		staticpage.admin.admin_man.cbacks [ 'edit_item' ] = function ( id ) { staticpage.admin.edit ( "am_footer", id ); };
		staticpage.admin.admin_man.cbacks [ 'del_item' ] = function ( id ) { staticpage.admin.del ( dest_div, id ); };
	}
};

staticpage.admin.list_row_manip = function ( ds, row )
{
	row [ '_id'  ] = row [ 'id' ];
	row [ '_created' ] = '';
	if ( row.get ( 'created' ) )
		row [ '_created' ] = liwe.utils.date2str ( row [ 'created' ], 1 );

	row [ '_visible' ] = row [ 'visible' ] == 1 ? 'si' : 'no';
};


staticpage.admin.set = function ( dest, data )
{
	staticpage.admin._dest = dest;
	var f = new liwe.form.instance ( 'static_page_form' );
	if ( ! data ) data = {};
	var vis = data.get ( "visible", -1 );
	
	f.hidden ( 'id_page', data.get ( 'id', '' ) );
	f.hidden ( 'id_html', data.get ( 'id_html', '' ) );
	f.hidden ( 'action', 'staticpage.admin.create' );
	f.text ( { name: 'name', label: 'Nome pagina', mandatory: true, value: data.get ( 'name', '' ), onblur: "staticpage.admin._parse_name()" } );
	f.text ( { name: 'title', label: 'HTML', mandatory: true, value: data.get ( 'title', '' ) } );
	f.jconed ( { 'name' : "html", width: 600, height: 400, value:  data.get ( 'html', '' ) } );
	//f.htmled ( { name: 'html', label: 'HTML', mandatory: true, value: data.get ( 'html', '' ), w: 600, h: 400 } );
	f.checkbox ( { name: 'visible', label: 'Visibile', checked: vis, value: 1 } );
	
	f.button ( { name: 'btn_page', value: 'Salva', onclick: "staticpage.admin.set_save()" } );

	f.set ( dest );
};

staticpage.admin._parse_name = function ()
{
	var f = liwe.form.get ( 'static_page_form' );
	var name = f.get_value ( 'name' );

	name = name.replace( /[^a-zA-Z 0-9 -_]+/g , '');
	name = name.replace ( / /g, '_' ).toLowerCase ();

	f.set_value ( 'name', name );
};

staticpage.admin.set_save = function ()
{
	var f = liwe.form.get ( 'static_page_form' );
	var dct = f.get_values ();

	liwe.AJAX.easy ( dct, function ( v ) {
		staticpage.admin.list ( staticpage.admin._dest );
	} );

};

staticpage.admin.list = function ( dest )
{
	staticpage.admin.admin_man.show_list ();
};

staticpage.admin.edit = function ( dest, id_page )
{
	liwe.AJAX.easy ( { 'action': 'staticpage.admin.get', id_page: id_page }, function ( v ) {
		staticpage.admin.set ( dest, v [ 'page' ] )
	} );
}; 

staticpage.admin.del = function ( dest, id_page )
{
	if ( ! id_page ) return;

	if ( confirm ( "Sei sicuro di voler eliminare questa pagina?" ) )
	{
		liwe.AJAX.easy ( { 'ids': id_page, 'action': "staticpage.admin.delete" }, function ( v ) {
			staticpage.admin.list ( dest );
		} );
	}
};

staticpage.admin._jconed_handler = function ()
{
	staticpage.admin._jconed_form ();
};

staticpage.admin._jconed_context = function ()
{
        var item = jconed.get_plugin_selection ();
        if ( ! item ) return;
	if ( ! liwe.dom.has_class ( item, 'liwe-plugin-staticpage' ) ) return;

        pre = item.getElementsByTagName ( "pre" );
        if ( ! pre.length ) return;

        pre = pre [ 0 ];

        eval ( 'var values = ' + pre.innerHTML );

	//if ( typeof ( values ) != 'object' ) return; 

	staticpage.admin._jconed_form ( values [ 'params' ] );
};

staticpage.admin._jconed_form = function ( values )
{
	values = values || {};
	var vals = { 
			action : 'staticpage.admin.list_names'
			};
	var opts = [];
	staticpage.admin.ajax ( vals, function ( res )
	{
		if ( res.pages.count () < 1 ) return;

		res.pages.iterate ( function ( v, k ) 
		{
			opts.push ( { label: v.title, value : v.name } );			
		} );
		var f = new liwe.form.instance ( 'jconed-staticpage' );

		f.hidden ( '_module', 'staticpage' );
                f.hidden ( '_title', staticpage.admin.module_name );
		f.select ( { name: 'name', options: opts, mandatory: true, label: 'Pagina', value: values.get ( 'name' ) } );
		f.button ( { value: "Conferma", onclick: "jconed.render_plugin('jconed-staticpage')" } );
		jconed.toolbar.set_context ( f.get () );
	} );
	
};
